/* JS Code for Search Bar Interactions */

$(function(){
	
	/*==============================
	== INTERACTIONS FOR SEARCH INPUT
	================================
	0. Init - Hide Label Text
	1. Focus - Fade Label Text
	2. Key - Hide Label Text
	3. Blur - Show Label Text
	4. Return - Trigger Submit
	==============================*/
	
	var searchform = $('#zi #searchform');
	var searchinput = searchform.find('.searchinput');
	var searchlabel = searchinput.find('label');
	var searchtext = searchinput.find('input.searchtext');
	
	// 0. Init - Hide Label Text (if search phrase)
	if(searchtext.val()){
		searchlabel.addClass('hide');
	}
	
	// 1. Focus - Fade Label Text
	searchtext.bind('focus',function(){
		searchlabel.addClass('fade');
	});

	// 2. Key - Hide Label Text
	searchtext.bind('keypress',function(){
		searchlabel.addClass('hide');
	});

	// 3. Blur - Show Label Text (if no search phrase)
	searchtext.bind('blur',function(){
		if($(this).val()==''){
			searchlabel.removeClass('fade').removeClass('hide');
		}
	});

	// 4. Return - Trigger Submit
	searchtext.bind('keypress',function(e){
		var code = (e.keyCode ? e.keyCode : e.which);
		if(code == 13) {
			searchform.trigger('submit');
		}
	});
	
	
	/*==============================
	== INTERACTIONS FOR DROP DOWN
	================================
	0. Init - Hide Last Menu Element
	1. Click - Toggle Menu and Change Value
	2. Mousleave - Hide Menu
	==============================*/
	
	var searchmenu = $('#zi .searchmenu');

	// 0. Init - Hide Last Menu Element
	searchmenu.find('li:last').addClass('last');
		
	// 1. Click - Toggle Menu
	searchmenu.bind('click',function(){

		// Show Hidden Menu
		if(!$(this).hasClass('show')){
			$(this).addClass('show');
		
		// Hide Menu
		}else{
			$(this).removeClass('show');
		}
	});
	
	// 1. Click - Change Value
	searchmenu.find('li').bind('click',function(e){
		e.preventDefault();
		
		// Set value to person
		if($(this).hasClass('people')){
			searchform.find('[name=t]').val('people');
			
			// Move top of menu if needed
			if($(this).index()!=0){
				searchmenu.prepend($(this));
				searchlabel.text('Enter person\'s name');
				searchtext.trigger('focus');
			}
		
		// Set value to companies
		}else if($(this).hasClass('companies')){
			searchform.find('[name=t]').val('companies');

			// Move top of menu if needed
			if($(this).index()!=0){
				searchmenu.prepend($(this));
				searchlabel.text('Enter company name');
				searchtext.trigger('focus');
			}
		}
		
		searchmenu.find('li:first').removeClass('last');
		searchmenu.find('li:last').addClass('last');
	});

	// 2. Mousleave - Hide Menu
	searchmenu.bind('mouseleave',function(){
		$(this).removeClass('show');
	});
	
	/*==============================
	== INTERACTIONS FOR SEARCH BUTTON
	================================
	1. Click - Submit Form
	==============================*/
	
	var searchbtn = searchform.find('.searchbutton');
	
	// 1. Click - Submit Form
	searchbtn.bind('click',function(e){
		e.preventDefault();
		
		searchform.trigger('submit');
	});
});